package dbexp.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Principal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Enumeration;
import java.util.EventListener;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.servlet.AsyncContext;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRegistration;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import javax.servlet.descriptor.JspConfigDescriptor;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import javax.servlet.http.Part;

import dbexp.connection.DbAccessException;
import dbexp.connection.DbMetaData;
import dbexp.connection.DbStateException;
import dbexp.connection.PoolManager;
import dbexp.connection.PoolManagerException;
import dbexp.connection.model.Client;
import dbexp.model.db.DbUser;
import dbexp.utils.StringUtil;

@SuppressWarnings("deprecation")
public class Static {
	private static Vector<Cookie> cookies = new Vector<Cookie>();
	private static DbUser p = p();
	private static boolean propLoaded = false;
	private static boolean initializedDb = false;
	private static String url = "index.jsp";
	private static Locale PL = new Locale("pl");
	private static String rp = "ext";
	private static String encoding = null;// "ISO-8859-2";
	private static Hashtable<String, Object> atrybuty = new Hashtable<String, Object>();
	private static Hashtable<String, Object> parametry = new Hashtable<String, Object>();
	private static Hashtable<String, Object> dateHeaders = new Hashtable<String, Object>();
	private static Hashtable<String, Object> headers = new Hashtable<String, Object>();
	@SuppressWarnings("unused")
	private static int STATUS = 200;
	@SuppressWarnings("unused")
	private static String STATUS_MESS = "";
	private static String STC = "";
	public static String KLASA = "";
	private static Vector<String> KLASY = new Vector<String>();
	private static boolean found = false;
	private static String DEFPATH = "ext/WEB-INF/classes/";

	/**
	 * Ustawia atrybut
	 * 
	 * @param key
	 * @param value
	 */
	public static void sa(String key, Object value) {
		if (value != null) {
			atrybuty.put(key, value);
		}
	}

	/**
	 * Ustawia String w hashu.<br>
	 * Jako wartość hasha w ostateczności otrzymywany jest Vector.(String);
	 * 
	 * @param hash
	 * @param key
	 * @param value
	 */
	@SuppressWarnings("unchecked")
	public static void se(Hashtable<String, Object> hash, String key,
			Object value) {
		if (hash.containsKey(key)) {
			Object o = hash.get(key);
			if (o instanceof String) {
				Vector<String> v = new Vector<String>();
				v.add(o.toString());
				v.add(value.toString());
				hash.put(key, v);
			} else if (o instanceof Vector<?>) {
				if (value != null) {
					if (key.equals("selected")) {
						Vector<String> v = new Vector<String>();
						v.add(value.toString());
						hash.put(key, v);
					} else {
						((Vector) o).add(value.toString());
					}
				}
			}
		} else {
			if (value != null) {
				if (value instanceof Vector) {
					hash.put(key, value);
				} else {
					Vector v = new Vector();
					v.add(value.toString());
					hash.put(key, v);
				}
			}
		}
	}

	/**
	 * Pobiera String z hasha.<br>
	 * .(String, String);-> (String)<br>
	 * .(String, vector.(String) );->(String)vector.get(0)
	 * 
	 * @param hash
	 * @param key
	 * @return
	 */
	public static String ge(Hashtable<String, Object> hash, String key) {
		Object o = hash.get(key);
		if (o instanceof String) {
			return (String) o;
		} else if (o instanceof Vector<?>) {
			Vector<?> v = (Vector<?>) o;
			if (v.size() == 0) {
				return null;
			}
			return (String) v.get(0);
		}
		return (String) hash.get(key);
	}

	/**
	 * Ustawia parametr
	 * 
	 * @param key
	 * @param value
	 * @see #setReportsFields(CentrapReport)
	 * @see Static
	 */
	public static void sp(String key, Object value) {
		se(parametry, key, value);
	}

	private static void sh(String key, Object value) {
		se(headers, key, value);
	}

	/**
	 * Ustawia nagłówek
	 * 
	 * @param key
	 * @param value
	 */
	private static void sdh(String key, Object value) {
		se(dateHeaders, key, value);
	}

	/**
	 * Pobiera atrybut
	 * 
	 * @param key
	 * @return
	 */
	public static Object ga(String key) {
		return atrybuty.get(key);
	}

	/**
	 * Pobiera parametr
	 * 
	 * @param key
	 * @return
	 */
	public static String gp(String key) {
		return ge(parametry, key);
	}

	/**
	 * Domyślne nagłównki oraz ciasteczko o sesji
	 */
	private static void i() {
		Cookie c = new Cookie("JSESSIONID", ses().getId());
		cookies.add(c);
		headers.put("host", "localhost");
		headers
				.put(
						"user-agent",
						"Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.6) Gecko/2009020414 CentOS/3.0.6-1.el5.centos Firefox/3.0.6");
		headers
				.put("accept",
						"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		headers.put("accept-language", "pl,en-us;q=0.7,en;q=0.3");
		headers.put("accept-encoding", "gzip,deflate");
		headers.put("accept-charset", "ISO-8859-2,utf-8;q=0.7,*;q=0.7");
		headers.put("keep-alive", "300");
		headers.put("connection", "keep-alive");
		headers.put("cookie", c.getName() + "=" + c.getValue());
	}

	/**
	 * Pobiera nagłówek
	 * 
	 * @param key
	 * @return
	 */
	private static String gH(String key) {
		i();
		return (String) headers.get(key);
	}

	/**
	 * Pobiera tablice stringów z Hasha:<br>
	 * .(String, String);->new String[1]{one}<br>
	 * .(String, Vector.(String));->new String[Vector.size]{vector.toArray}
	 * 
	 * @param hash
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static String[] gt(Hashtable<String, Object> hash, String key) {
		Object o = hash.get(key);
		String[] out = new String[0];
		if (o instanceof Vector<?>) {
			out = new String[((Vector) o).size()];
			((Vector) o).toArray(out);
		} else if (o instanceof String) {
			out = new String[] { o.toString() };
		}
		return out;
	}

	static ServletInputStream sis = new ServletInputStream() {
		@Override
		public int read(byte[] b) throws IOException {
			return System.in.read(b);
		}

		@Override
		public int read(byte[] b, int off, int len) throws IOException {
			return System.in.read(b, off, len);
		}

		@Override
		public synchronized void reset() throws IOException {
			System.in.reset();
		}

		@Override
		public long skip(long n) throws IOException {
			return System.in.skip(n);
		}

		@Override
		public int available() throws IOException {
			return super.available();
		}

		@Override
		public void close() throws IOException {
			System.in.close();
		}

		@Override
		public synchronized void mark(int readlimit) {
			System.in.mark(readlimit);
		}

		@Override
		public boolean markSupported() {
			return System.in.markSupported();
		}

		@Override
		public int read() throws IOException {
			return System.in.read();
		}
	};
	static ServletOutputStream sos = new ServletOutputStream() {
		@Override
		public void print(boolean b) throws IOException {
			System.out.print(b);
		}

		@Override
		public void print(char c) throws IOException {
			System.out.print(c);
		}

		@Override
		public void print(double d) throws IOException {
			System.out.print(d);
		}

		@Override
		public void print(float f) throws IOException {
			System.out.print(f);
		}

		@Override
		public void print(int i) throws IOException {
			System.out.print(i);
		}

		@Override
		public void print(long l) throws IOException {
			System.out.print(l);
		}

		@Override
		public void print(String s) throws IOException {
			System.out.print(s);
		}

		@Override
		public void println() throws IOException {
			System.out.println();
		}

		@Override
		public void println(boolean b) throws IOException {
			System.out.println(b);
		}

		@Override
		public void println(char c) throws IOException {
			System.out.println(c);
		}

		@Override
		public void println(double d) throws IOException {
			System.out.println(d);
		}

		@Override
		public void println(float f) throws IOException {
			System.out.println(f);
		}

		@Override
		public void println(int i) throws IOException {
			System.out.println(i);
		}

		@Override
		public void println(long l) throws IOException {
			System.out.println(l);
		}

		@Override
		public void println(String s) throws IOException {
			System.out.println(s);
		}

		@Override
		public void write(int b) throws IOException {
			System.out.print((char) b);
		}
	};
	/**
	 * HttpServletRequest we własnej implementacji
	 */
	static HttpServletRequest req = new HttpServletRequest() {
		@Override
		public String getAuthType() {
			return "BASIC_AUTH";
		}

		@Override
		public String getContextPath() {
			return "/CentralReports";
		}

		@Override
		public Cookie[] getCookies() {
			i();
			return cookies.toArray(new Cookie[cookies.size()]);
		}

		@Override
		public long getDateHeader(String name) {
			i();
			Object o = dateHeaders.get(name);
			if (o == null) {
				return -1;
			} else {
				return Long.valueOf(o.toString()).longValue();
			}
		}

		@Override
		public String getHeader(String name) {
			return gH(name);
		}

		@Override
		public Enumeration<String> getHeaderNames() {
			i();
			return headers.keys();
		}

		@Override
		public Enumeration<String> getHeaders(String name) {
			i();
			return StringUtil.getVector(gt(headers, name)).elements();
		}

		@Override
		public int getIntHeader(String name) {
			i();
			Object o = headers.get(name);
			if (o == null) {
				return -1;
			} else {
				try {
					return Integer.parseInt(o.toString());
				} catch (NumberFormatException e) {
					return -1;
				}
			}
		}

		@Override
		public String getMethod() {
			return "GET";
		}

		@Override
		public String getPathInfo() {
			return null;
		}

		@Override
		public String getPathTranslated() {
			return null;
		}

		@Override
		public String getQueryString() {
			Enumeration<String> e = getParameterNames();
			String out = "";
			while (e.hasMoreElements()) {
				String o = e.nextElement();
				String v = getParameter(o);
				if (out.trim().length() > 0) {
					out += "&";
				}
				out += o + "=" + v;
			}
			return out;
		}

		@Override
		public String getRemoteUser() {
			return null;
		}

		@Override
		public String getRequestURI() {
			return getContextPath() + "/" + getServletPath();
		}

		@Override
		public StringBuffer getRequestURL() {
			return new StringBuffer(getScheme() + "://" + getServerName()
					+ ((getServerPort() == 80) ? "" : ":" + getServerPort())
					+ getRequestURI());
		}

		@Override
		public String getRequestedSessionId() {
			return ses().getId();
		}

		@Override
		public String getServletPath() {
			return url();
		}

		@Override
		public HttpSession getSession() {
			return ses();
		}

		@Override
		public HttpSession getSession(boolean create) {
			return ses(create);
		}

		@Override
		public Principal getUserPrincipal() {
			return null;
		}

		@Override
		public boolean isRequestedSessionIdFromCookie() {
			return true;
		}

		@Override
		public boolean isRequestedSessionIdFromURL() {
			return false;
		}

		@Override
		public boolean isRequestedSessionIdFromUrl() {
			return false;
		}

		@Override
		public boolean isRequestedSessionIdValid() {
			return true;
		}

		@Override
		public boolean isUserInRole(String role) {
			return false;
		}

		@Override
		public Object getAttribute(String name) {
			return ga(name);
		}

		@Override
		public Enumeration<String> getAttributeNames() {
			return atrybuty.keys();
		}

		@Override
		public String getCharacterEncoding() {
			return encoding;
		}

		@Override
		public int getContentLength() {
			return -1;
		}

		@Override
		public String getContentType() {
			return null;
		}

		@Override
		public ServletInputStream getInputStream() throws IOException {
			return sis();
		}

		@Override
		public String getLocalAddr() {
			return "127.0.0.1";
		}

		@Override
		public String getLocalName() {
			return "localhost";
		}

		@Override
		public int getLocalPort() {
			return 80;
		}

		@Override
		public Locale getLocale() {
			return PL;
		}

		@Override
		public Enumeration<Locale> getLocales() {
			Vector<Locale> v = new Vector<Locale>(3);
			v.add(PL);
			v.add(Locale.US);
			v.add(Locale.ENGLISH);
			return v.elements();
		}

		@Override
		public String getParameter(String name) {
			return gp(name);
		}

		@Override
		public Map<String, String[]> getParameterMap() {
			return null;// parametry;
		}

		@Override
		public Enumeration<String> getParameterNames() {
			return parametry.keys();
		}

		@Override
		public String[] getParameterValues(String name) {
			return gt(parametry, name);
		}

		@Override
		public String getProtocol() {
			return "HTTP/1.1";
		}

		@Override
		public BufferedReader getReader() throws IOException {
			return null;
		}

		@Override
		public String getRealPath(String path) {
			return rp() + path;
		}

		@Override
		public String getRemoteAddr() {
			return getLocalAddr();
		}

		@Override
		public String getRemoteHost() {
			return getLocalAddr();
		}

		@Override
		public int getRemotePort() {
			return 22488;
		}

		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return null;
		}

		@Override
		public String getScheme() {
			return "http";
		}

		@Override
		public String getServerName() {
			return "localhost";
		}

		@Override
		public int getServerPort() {
			return 80;
		}

		@Override
		public boolean isSecure() {
			return false;
		}

		@Override
		public void removeAttribute(String name) {
			atrybuty.remove(name);
			parametry.remove(name);
		}

		@Override
		public void setAttribute(String name, Object o) {
			sa(name, o);
			sp(name, o);
		}

		@Override
		public void setCharacterEncoding(String env)
				throws UnsupportedEncodingException {
			encoding = env;
		}

		@Override
		public AsyncContext getAsyncContext() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public DispatcherType getDispatcherType() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ServletContext getServletContext() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean isAsyncStarted() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isAsyncSupported() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public AsyncContext startAsync() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public AsyncContext startAsync(ServletRequest arg0, ServletResponse arg1) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean authenticate(HttpServletResponse arg0)
				throws IOException, ServletException {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public Part getPart(String arg0) throws IOException,
				IllegalStateException, ServletException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Collection<Part> getParts() throws IOException,
				IllegalStateException, ServletException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void login(String arg0, String arg1) throws ServletException {
			// TODO Auto-generated method stub

		}

		@Override
		public void logout() throws ServletException {
			// TODO Auto-generated method stub

		}
	};
	/**
	 * HttpServletResponse we własnej implementacji
	 */
	static HttpServletResponse res = new HttpServletResponse() {
		@Override
		public void addCookie(Cookie cookie) {
			cookies.add(cookie);
		}

		@Override
		public void addDateHeader(String name, long date) {
			sdh(name, new Long(date));
		}

		@Override
		public void addHeader(String name, String value) {
			sh(name, value);
		}

		@Override
		public void addIntHeader(String name, int value) {
			sh(name, new Integer(value));
		}

		@Override
		public boolean containsHeader(String name) {
			return headers.containsKey(name);
		}

		@Override
		public void sendError(int sc) throws IOException {
			STATUS = sc;
		}

		@Override
		public void sendError(int sc, String msg) throws IOException {
			STATUS = sc;
			STATUS_MESS = msg;
		}

		@Override
		public void sendRedirect(String location) throws IOException {
			url = location;
		}

		@Override
		public void setDateHeader(String name, long date) {
			sdh(name, new Long(date));
		}

		@Override
		public void setHeader(String name, String value) {
			sh(name, value);
		}

		@Override
		public void setIntHeader(String name, int value) {
			sh(name, new Integer(value));
		}

		@Override
		public void setStatus(int sc) {
			STATUS = sc;
		}

		@Override
		public void setStatus(int sc, String sm) {
			STATUS = sc;
		}

		@Override
		public void flushBuffer() throws IOException {
		}

		@Override
		public int getBufferSize() {
			return 0;
		}

		@Override
		public String getCharacterEncoding() {
			return encoding;
		}

		@Override
		public String getContentType() {
			return null;
		}

		@Override
		public Locale getLocale() {
			return PL;
		}

		@Override
		public ServletOutputStream getOutputStream() throws IOException {
			return sos();
		}

		@Override
		public PrintWriter getWriter() throws IOException {
			return null;
		}

		@Override
		public boolean isCommitted() {
			return false;
		}

		@Override
		public void reset() {
		}

		@Override
		public void resetBuffer() {
		}

		@Override
		public void setBufferSize(int size) {
		}

		@Override
		public void setCharacterEncoding(String charset) {
			encoding = charset;
		}

		@Override
		public void setContentLength(int len) {
		}

		@Override
		public void setContentType(String type) {
		}

		@Override
		public void setLocale(Locale loc) {
			PL = loc;
		}

		@Override
		public String encodeRedirectURL(String arg0) {
			return null;
		}

		@Override
		public String encodeRedirectUrl(String arg0) {
			return null;
		}

		@Override
		public String encodeURL(String arg0) {
			return null;
		}

		@Override
		public String encodeUrl(String arg0) {
			return null;
		}

		@Override
		public String getHeader(String arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Collection<String> getHeaderNames() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Collection<String> getHeaders(String arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int getStatus() {
			// TODO Auto-generated method stub
			return 0;
		}
	};
	static ServletContext sc = new ServletContext() {
		@Override
		public Object getAttribute(String name) {
			return ga(name);
		}

		@Override
		public Enumeration<String> getAttributeNames() {
			return atrybuty.keys();
		}

		@Override
		public ServletContext getContext(String uripath) {
			return this;
		}

		@Override
		public String getInitParameter(String name) {
			return null;
		}

		@Override
		public Enumeration<String> getInitParameterNames() {
			return null;
		}

		@Override
		public int getMajorVersion() {
			return 0;
		}

		@Override
		public String getMimeType(String file) {
			return null;
		}

		@Override
		public int getMinorVersion() {
			return 0;
		}

		@Override
		public RequestDispatcher getNamedDispatcher(String name) {
			return null;
		}

		@Override
		public String getRealPath(String path) {
			return rp() + path;
		}

		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return null;
		}

		@Override
		public URL getResource(String path) throws MalformedURLException {
			return null;
		}

		@Override
		public InputStream getResourceAsStream(String path) {
			return null;
		}

		@Override
		public Set<String> getResourcePaths(String path) {
			return null;
		}

		@Override
		public String getServerInfo() {
			return null;
		}

		@Override
		public Servlet getServlet(String name) throws ServletException {
			return null;
		}

		@Override
		public String getServletContextName() {
			return null;
		}

		@Override
		public Enumeration<String> getServletNames() {
			return null;
		}

		@Override
		public Enumeration<Servlet> getServlets() {
			return null;
		}

		@Override
		public void log(String msg) {
			System.err.println(msg);
		}

		@Override
		public void log(Exception exception, String msg) {
			System.err.println(exception + ":" + msg);
		}

		@Override
		public void log(String message, Throwable throwable) {
			System.err.println(message + ":" + throwable);
		}

		@Override
		public void removeAttribute(String name) {
			atrybuty.remove(name);
		}

		@Override
		public void setAttribute(String name, Object object) {
			sa(name, object);
		}

		@Override
		public String getContextPath() {
			return null;
		}

		@Override
		public Dynamic addFilter(String arg0, String arg1) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Dynamic addFilter(String arg0, Filter arg1) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Dynamic addFilter(String arg0, Class<? extends Filter> arg1) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void addListener(Class<? extends EventListener> arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void addListener(String arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public <T extends EventListener> void addListener(T arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public javax.servlet.ServletRegistration.Dynamic addServlet(
				String arg0, String arg1) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public javax.servlet.ServletRegistration.Dynamic addServlet(
				String arg0, Servlet arg1) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public javax.servlet.ServletRegistration.Dynamic addServlet(
				String arg0, Class<? extends Servlet> arg1) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public <T extends Filter> T createFilter(Class<T> arg0)
				throws ServletException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public <T extends EventListener> T createListener(Class<T> arg0)
				throws ServletException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public <T extends Servlet> T createServlet(Class<T> arg0)
				throws ServletException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void declareRoles(String... arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public ClassLoader getClassLoader() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int getEffectiveMajorVersion() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public int getEffectiveMinorVersion() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public FilterRegistration getFilterRegistration(String arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public JspConfigDescriptor getJspConfigDescriptor() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ServletRegistration getServletRegistration(String arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Map<String, ? extends ServletRegistration> getServletRegistrations() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public SessionCookieConfig getSessionCookieConfig() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public boolean setInitParameter(String arg0, String arg1) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void setSessionTrackingModes(Set<SessionTrackingMode> arg0)
				throws IllegalStateException, IllegalArgumentException {
			// TODO Auto-generated method stub

		}
	};
	/**
	 * HttpSession we własnej implementacji
	 */
	static HttpSession ses = newSession();

	private static HttpSession newSession() {
		return new HttpSession() {
			private Hashtable<String, Object> superOpcje = new Hashtable<String, Object>();
			private String id = String.valueOf(Math.random());
			private long creationTime = System.currentTimeMillis();
			private long lastAccess = creationTime;
			private int maxInactiveInterval = 0;
			private boolean valid = true;
			private boolean isNew = true;

			@Override
			public Object getAttribute(String name) {
				return g(name);
			}

			private void v() {
				if (!valid) {
					throw new IllegalStateException();
				}
				lastAccess = System.currentTimeMillis();
				isNew = false;
			}

			public void s(String key, Object value) {
				v();
				if (value != null) {
					superOpcje.put(key, value);
				}
			}

			private Object g(String key) {
				v();
				return superOpcje.get(key);
			}

			@Override
			public Enumeration<String> getAttributeNames() {
				v();
				return superOpcje.keys();
			}

			@Override
			public long getCreationTime() {
				v();
				return creationTime;
			}

			@Override
			public String getId() {
				v();
				return id;
			}

			@Override
			public long getLastAccessedTime() {
				v();
				return lastAccess;
			}

			@Override
			public int getMaxInactiveInterval() {
				v();
				return maxInactiveInterval;
			}

			@Override
			public ServletContext getServletContext() {
				v();
				return sc();
			}

			@Override
			public Object getValue(String name) {
				return g(name);
			}

			@Override
			public String[] getValueNames() {
				v();
				return (String[]) superOpcje.keySet().toArray();
			}

			@Override
			public void invalidate() {
				valid = false;
			}

			@Override
			public boolean isNew() {
				boolean out = isNew;
				isNew = false;
				return out;
			}

			@Override
			public void putValue(String name, Object value) {
				v();
				s(name, value);
			}

			@Override
			public void removeAttribute(String name) {
				v();
				superOpcje.remove(name);
			}

			@Override
			public void removeValue(String name) {
				v();
				superOpcje.remove(name);
			}

			@Override
			public void setAttribute(String name, Object value) {
				v();
				s(name, value);
			}

			@Override
			public void setMaxInactiveInterval(int interval) {
				v();
				maxInactiveInterval = interval;
			}

			@Override
			public HttpSessionContext getSessionContext() {
				v();
				return null;
			}
		};
	}

	/**
	 * Zwraca aktualną sesje.<br>
	 * Tworzy w przypadku gdy sesja jest niewa�na( invalidate() )
	 * 
	 * @return
	 */
	public static HttpSession ses() {
		return ses(true);
	}

	/**
	 * Zwraca aktualną sesje.<br>
	 * Tworzy lub nie w zależności od parametru create
	 * 
	 * @see #ses()
	 * @param create
	 * @return
	 */
	public static HttpSession ses(boolean create) {
		try {
			ses.getId();
		} catch (IllegalStateException e) {
			if (create) {
				ses = newSession();
			} else {
				ses = null;
			}
		}
		if ((ses == null) && create) {
			ses = newSession();
		}
		return ses;
	}

	/**
	 * Zwraca aktualny ServletInputStream
	 * 
	 * @return
	 */
	public static ServletInputStream sis() {
		return sis;
	}

	/**
	 * Zwraca aktualny ServletOutputStream
	 * 
	 * @return
	 */
	public static ServletOutputStream sos() {
		return sos;
	}

	/**
	 * Zwraca aktualny ServletContext
	 * 
	 * @return
	 */
	public static ServletContext sc() {
		return sc;
	}

	/**
	 * Zwraca aktualny HttpServletRequest
	 * 
	 * @return
	 */
	public static HttpServletRequest req() {
		return req;
	}

	/**
	 * Zwraca aktualny HttpServletResponse
	 * 
	 * @return
	 */
	public static HttpServletResponse res() {
		return res;
	}

	/**
	 * Zwraca aktualny url strony
	 * 
	 * @return
	 */
	public static String url() {
		return url;
	}

	/**
	 * Zwraca realPath - bezwzględną ścieżkę kontekstu aplikacji
	 * 
	 * @return
	 */
	public static String rp() {
		return rp;
	}

	/**
	 * Tworzy nowe połączenie z bazą
	 * 
	 * @param connectionInfo
	 * @return
	 */
	protected static Connection silentConnection(Client connectionInfo) {
		try {
			return PoolManager.getInstance().getConnection(connectionInfo);
		} catch (Exception e) {
		}
		return null;
	}

	public static void init() {
		ss("USER", p);
	}

	public static boolean isPropLoaded() {
		return propLoaded;
	}

	public static boolean isInitializedDb() {
		return initializedDb;
	}

	/**
	 * Zwraca zalogowaną osobę
	 * 
	 * @return
	 */
	public static DbUser p() {
		if (p == null) {
			try {
				p = DbMetaData.createConnection(new DbUser("ORA9I",
						"localhost", "qazxsw", 1521, "SYSTEM"));
			} catch (PoolManagerException e) {
				e.printStackTrace();
			} catch (DbStateException e) {
				e.printStackTrace();
			} catch (DbAccessException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return p;
	}

	/**
	 * Drukuje tablice
	 * 
	 * @param t
	 */
	public static void pt(String[] t) {
		for (String element : t) {
			s(element);
		}
	}

	/**
	 * Drukuje elementy wyliczenia
	 * 
	 * @param e
	 */
	public static void pe(Enumeration<?> e) {
		while (e.hasMoreElements()) {
			Object object = e.nextElement();
			s(object);
		}
	}

	/**
	 * Drukuje zawartość mapy
	 * 
	 * @param m
	 */
	public static void pm(Map<?, ?> m) {
		Iterator<?> i = m.keySet().iterator();
		while (i.hasNext()) {
			String key = (String) i.next();
			Object v = m.get(key);
			s(v);
		}
	}

	/**
	 * Ustawia obiekt w sesji oraz żądaniu
	 * 
	 * @param name
	 * @param value
	 */
	public static void s(String name, String value) {
		Static.req.setAttribute(name, value);
	}

	/**
	 * Ustawia obiekt tylko w zasięgu sesji
	 * 
	 * @param name
	 * @param value
	 */
	public static void ss(String name, Object value) {
		Static.ses.setAttribute(name, value);
	}

	/**
	 * Drukuje obiekt z parametru
	 * 
	 * @param o
	 */
	public static void s(Object o) {
		System.out.println(o);
	}

	/**
	 * Pobiera parametr żądania
	 * 
	 * @param key
	 * @return
	 */
	public static Object g(String key) {
		return Static.req.getParameter(key);
	}

	/**
	 * Pobiera obiekt sesji
	 * 
	 * @param key
	 * @return
	 */
	public static Object gs(String key) {
		return Static.ses.getAttribute(key);
	}

	@Override
	protected void finalize() throws Throwable {
		super.finalize();
	}

	/**
	 * Szuka klasy o zadanej nazwie.
	 * 
	 * @param className
	 * @return
	 */
	public static Class<?> findClass(String className) {
		sstc(className);
		File sciezkaKlas = new File(DEFPATH);
		readDirectory(sciezkaKlas, className);
		if (KLASA.trim().length() > 0) {
			KLASA = KLASA.replaceFirst(DEFPATH, "");
			KLASA = KLASA.replaceFirst(".class", "");
			KLASA = KLASA.replaceAll("/", ".");
			try {
				return Class.forName(KLASA);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * Czyta katalog w poszukiwaniu pliku myFile
	 * 
	 * @param f
	 * @param myClass
	 */
	public static void readDirectory(File f, String myFile) {
		readDirectory(new File[] { f }, myFile);
	}

	/**
	 * Czyta rekurencyjnie pliki/katalogi
	 * 
	 * @param fs
	 * @param myClass
	 */
	public static void readDirectory(File[] fs, String myFile) {
		if ((fs != null) && (fs.length > 0) && !found) {
			for (int i = 0; (i < fs.length) && !found; i++) {
				File f = fs[i];
				if (f.exists() && !found) {
					if (f.isDirectory() && !found) {
						readDirectory(f.listFiles(), myFile);
					} else if ((f.getName().indexOf(myFile) != -1)
							&& (f.getName().indexOf("$") == -1)) {
						found = true;
						KLASA = f.getAbsolutePath();
					}
				}
			}
		}
	}

	/**
	 * Szuka klas o zadanej nazwie.
	 * 
	 * @param className
	 * @return
	 */
	public static Vector<String> findAClass(String className) {
		sstc(className);
		File sciezkaKlas = new File(DEFPATH);
		readDirectoryFAF(sciezkaKlas, className);
		return KLASY;
	}

	/**
	 * Konwertuje nazwę pliku na klasę
	 * 
	 * @param fileName
	 * @return
	 */
	@SuppressWarnings("unused")
	private static Class<?> getClassFromFileName(String fileName) {
		if ((fileName != null) && (fileName.trim().length() > 0)) {
			fileName = fileName.replaceFirst(DEFPATH, "");
			fileName = fileName.replaceFirst(".class", "");
			fileName = fileName.replaceAll("/", ".");
			try {
				return Class.forName(fileName);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * Czyta katalog w poszukiwaniu plików, które mają w nazwie myFile
	 * 
	 * @param f
	 * @param myFile
	 */
	public static void readDirectoryFAF(File f, String myFile) {
		readDirectoryFAF(new File[] { f }, myFile);
	}

	/**
	 * Czyta rekurencyjnie pliki/katalogi
	 * 
	 * @param fs
	 * @param myClass
	 */
	public static void readDirectoryFAF(File[] fs, String myFile) {
		if ((fs != null) && (fs.length > 0)) {
			for (File f : fs) {
				if (f.exists()) {
					if (f.isDirectory()) {
						readDirectoryFAF(f.listFiles(), myFile);
					} else if ((f.getName().indexOf(myFile) != -1)
							&& (f.getName().indexOf("$") == -1)) {
						KLASY.add(f.getAbsolutePath());
					}
				}
			}
		}
	}

	private static void sstc(String t) {
		if ((STC != null) && (STC.trim().length() == 0)) {
			STC = t;
		}
	}
}
